<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-row>
      <el-col :span="24">
        <el-button type="danger" style="float: right" size="small" icon="el-icon-close" circle @click="doCancelEdit"></el-button>
      </el-col>
    </el-row>
    <el-form class="tl-form" ref="dealform" :model="model" :rules="rules" label-width="120px">
    </el-form>
    <el-alert title="请填写任命书信息，任命项目组后，再点击办理通过！" type="warning" show-icon :closable="false">
    </el-alert>
    <el-tabs v-model="activeTabName" type="card" @tab-click="tabClick">
      <el-tab-pane label="任命书" name="basic">
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="任命书标题" prop="appointmenttitle" :rules="rules.validateRequire">
                <el-input v-model="model.appointmenttitle" :disabled="true"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="任命书编号" prop="appointmentnumber" :rules="rules.validateRequire">
                <el-input v-model="model.appointmentnumber" :disabled="true"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="签发人" prop="signercode" :rules="rules.validateRequire">
                <tl-ref-user v-model="model.signercode" :initDisplayData.sync="model.signername" dataType="code" :singleSelect="true">
                </tl-ref-user>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span=" 24">
              <el-form-item label="任命书描述" prop="appointmentdescription" :rules="rules.validateRequire">
                <el-input v-model="model.appointmentdescription" type="textarea" maxlength="1000" show-word-limit :rows="8"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-collapse>
            <el-collapse-item title="【单据】申请信息">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="申请人姓名" prop="applyusername" :rules="rules.validateRequire">
                    <el-input v-model="model.applyusername" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请人工号" prop="applyusercode">
                    <el-input v-model="model.applyusercode" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="申请人部门" prop="applydeptname">
                    <el-input v-model="model.applydeptname" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请人公司" prop="applyorgname">
                    <el-input v-model="model.applyorgname" :disabled="true"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-collapse-item>
          </el-collapse>

        </el-form>
      </el-tab-pane>
      <el-tab-pane label="项目组" name="line">
        <tl-line ref="line" :parentId="model.id" :projectCode="model.projectcode"></tl-line>
      </el-tab-pane>
    </el-tabs>
    <tl-approval-history ref="history" sourceType="MpmProjectAppointment" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <!-- <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit"
        :disabled="disableSave">
        <svg-icon icon-class="save" />保存
      </el-button>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit"
        :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button> -->
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if=" this.activeTabName === 'basic'">
        <svg-icon icon-class="save" />保存
      </el-button>
      <tl-attachment sourceType="MpmProjectAppointment" :sourceId="model.id" :disabled="disableAttachment" :editable="true"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button>
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlLine from '../mpmProjectTeam/indexFrame'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlLine },
  data() {
    return {
      moduleTitle: '项目任命',
      urlPrefix: '/mpm/projectAppointment/',
      url: {
        // 额外的url
      },
      activeTabName: 'basic',
      isRefreshLine: false,
      // 审批节点配置的额外参数
      extraParam: '',
      // 判断是否是审批组件调用
      fromApproval: ''
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'applyorgcode', this.org.code)
        this.$set(this.model, 'applyorgname', this.org.name)
        this.$set(this.model, 'applydeptcode', this.dept.code)
        this.$set(this.model, 'applydeptname', this.dept.fullname)
        this.$set(this.model, 'applyusercode', this.user.code)
        this.$set(this.model, 'applyusername', this.user.name)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
      }
    },
    loaded() {
      // 页面加载完毕执行
      this.activeTabName = 'basic'
      this.isRefreshLine = false
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    tabClick(tab) {
      if (tab.name === 'line') {
        if (!this.model.id) {
          this.$nextTick(_ => {
            this.activeTabName = 'basic'
          })
          this.$tap
            .confirm('当前数据未保存，是否立即保存数据？')
            .then(_ => {
              this.doSubmitEdit()
            })
            .catch(_ => { })
        } else if (!this.isRefreshLine) {
          this.$refs.line.doQuery()
          this.isRefreshLine = true
        }
        this.$refs.line.initPage()
      }
    },
    saveCourierInfo(resolve, reject) {
      const { id, appointmenttitle, appointmentnumber, signercode, signername, appointmentdescription } = this.model
      if (!appointmenttitle || !signercode || !signername || !appointmentdescription) {
        this.$tap.alert('请填写任命书相关信息并任命项目组成员!', 'warning')
        reject()
      }
      const param = {
        id,
        appointmenttitle,
        appointmentnumber,
        signercode,
        signername,
        appointmentdescription
      }
      putAction(this.url.edit, param)
        .then(res => {
          if (res.success) {
            resolve()
          } else {
            this.$tap.alert(res.message, 'warning')
            reject()
          }
        })
        .catch(err => {
          this.$tap.alert('保存任命书信息失败，请联系系统管理员', 'warning')
          reject()
        })
    },
    doCancelEdit() {
      this.visible = false
      this.$emit('close')
      this.$emit('refresh')
    }
  }
}
</script>

